package 数据结构OJ;

/**
 * @author shy_black
 * @date 2019/6/26 11:27
 * @Description:
 */
public class 字符流中第一个不重复的字符 {
    //用作记录字符是否出现，及出现次数
    int[] tmp = new int[256];
    //存储于字符流
    StringBuffer sb = new StringBuffer();

    //Insert one char from stringstream
    public void Insert(char ch) {
        //存储字符流
        sb.append(ch);
        if (tmp[ch] == 0) {
            tmp[ch] = 1;
        } else {
            tmp[ch] += 1;
        }
    }

    //return the first appearence once char in current stringstream
    public char FirstAppearingOnce() {
        //str-->实际的字符串，转换为字符数组
        char[] str = sb.toString().toCharArray();
        //遍历字符数组，和记录的辅助数组协同判断，如果字符只出现一次，则返回字符本身

        for (char c : str) {
            if (tmp[c] == 1)
                return c;
        }
        //字符流中没有存在一次的字符，返回‘#’
        return '#';
    }
}